package ar.Location.Weather;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class WeatherHandler extends DefaultHandler {

	private static final String TAG = "WeatherHandler";
	private String host = "https://www.google.com";

	boolean is_entered_forecast_information_TAG = false;
	boolean is_entered_current_conditions_TAG = false;
	boolean is_entered_forecast_conditions_TAG = false;

	Weather weather = null;
	ForecastConditions forecastConditions = null;
	StringBuffer stringBuffer = new StringBuffer();

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		weather = new Weather();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub

		stringBuffer.setLength(0);

		String value = attributes.getValue("data");
		Log.d(TAG, "Val=" + value);

		if (localName.equals("forecast_information")) {
			is_entered_forecast_information_TAG = true;
		}
		if (localName.equals("current_conditions")) {
			is_entered_current_conditions_TAG = true;
		}
		if (localName.equals("forecast_conditions")) {
			is_entered_forecast_conditions_TAG = true;
			forecastConditions = new ForecastConditions();
		}

		if (is_entered_forecast_information_TAG) {

			if (localName.equals("city")) {
				weather.forecastInformation.city = value;
			}
			if (localName.equals("forecast_date")) {
				weather.forecastInformation.forecastDate = value;
			}
			if (localName.equals("current_date_time")) {
				weather.forecastInformation.currentDateTime = value;
			}
		}

		if (is_entered_current_conditions_TAG) {

			if (localName.equals("condition")) {
				weather.currentConditions.condition = value;
			}
			if (localName.equals("temp_c")) {
				weather.currentConditions.temperature = value;
			}
			if (localName.equals("humidity")) {
				weather.currentConditions.humidity = value;
			}
			if (localName.equals("icon")) {
				weather.currentConditions.iconUrl = value;
				if (!weather.currentConditions.iconUrl
						.startsWith("https://www.google.com")) {
					weather.currentConditions.iconUrl = host
							+ weather.currentConditions.iconUrl;
				}
			}
			if (localName.equals("wind_condition")) {
				weather.currentConditions.windCondition = value;
			}
		}

		if (is_entered_forecast_conditions_TAG) {

			if (localName.equals("day_of_week")) {
				forecastConditions.dayOfWeek = value;
			}
			if (localName.equals("low")) {
				forecastConditions.lowTemperature = value;
			}
			if (localName.equals("high")) {
				forecastConditions.highTemperature = value;
			}
			if (localName.equals("icon")) {
				forecastConditions.iconUrl = value;
				if (!forecastConditions.iconUrl
						.startsWith("https://www.google.com")) {
					forecastConditions.iconUrl = host
							+ forecastConditions.iconUrl;
				}
			}
			if (localName.equals("condition")) {
				forecastConditions.condition = value;
			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		stringBuffer.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if (localName.equals("forecast_information")) {
			is_entered_forecast_information_TAG = false;
		}
		if (localName.equals("current_conditions")) {
			is_entered_current_conditions_TAG = false;
		}

		if (localName.equals("forecast_conditions")) {
			is_entered_forecast_conditions_TAG = false;
			weather.listConditions.add(forecastConditions);
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	public Weather getWeather() {
		if (weather.listConditions != null
				&& weather.listConditions.size() == 4) {
			return weather;
		}
		return null;
	}
}
